﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using LibraryO2S;
using System.IO;
using BusinessObjects;

public partial class controls_admin_AdvPositionAction : Pages_Base
{
    private DataTable objDT;
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            loadSetting();
            objDT = BO_Module.SelectByType(5);
            foreach (DataRow dr in objDT.Rows)
                ddlModule.Items.Add(new ListItem(dr["FULL_NAME"].ToString(), dr["MODULE_ID"].ToString()));
            objDT = BO_Module.SelectByType(10);
            foreach (DataRow dr in objDT.Rows)
                ddlModule.Items.Add(new ListItem(dr["FULL_NAME"].ToString(), dr["MODULE_ID"].ToString()));
            //loadModule();
            if (Request["m_id"] != null)
            {
                string m_id = Request["m_id"].ToString();
                ddlModule.SelectedValue = m_id;
            }
            if (Request["a_id"] != null)
            {
                loadAdv(Convert.ToInt32(Request["a_id"]));
                btnSubmit.Text = "Cập Nhật";
            }
            else
            {
                btnSubmit.Text = "Thêm Mới";
                imgAdv.Style.Clear();
                imgAdv.Style.Add("display", "none");
                cldEndDate.SelectedDate = DateTime.Now;
            }
        }

    }
    private void loadAdv(int adv_id)
    {
        try
        {
            base.OpenConnection();
            DataTable objDT = BO_Ads_Picture.Search(adv_id);
            if (objDT.Rows.Count > 0)
            {
                imgAdv.Style.Clear();
                imgAdv.Style.Add("display", "block");
                imgAdv.Src = "../../upload/ads/" + objDT.Rows[0]["URL"].ToString();
                txtTitle.Text = objDT.Rows[0]["PICTURE_NAME_VN"].ToString();
                txtUrl.Text = objDT.Rows[0]["TARGET_LINK"].ToString();
                cldEndDate.SelectedDate = Convert.ToDateTime(objDT.Rows[0]["END_DATE"]);
                chbDisplay.Checked = Convert.ToBoolean(objDT.Rows[0]["IS_DISPLAY_ON_HOME_PAGE"]);
                txtSortDisplay.Text = objDT.Rows[0]["SORT_DISPLAY"].ToString();
                ddlModule.SelectedValue = objDT.Rows[0]["MODULE_ID"].ToString();
            }
        }
        catch (Exception ex)
        {
            //throw ex;
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi trong quá trình xử lý<br />" + ex.Message);
        }
        finally { base.CloseConnection(); }
    }
    //protected void loadModule()
    //{
    //    try
    //    {
    //        base.OpenConnection();
    //        DataTable moduleDT = BO_Module.SelectType();
    //        if (moduleDT != null && moduleDT.Rows.Count > 0)
    //        {
    //            ddlModule.DataSource = moduleDT;
    //            ddlModule.DataTextField = "FULL_NAME";
    //            ddlModule.DataValueField = "MODULE_ID";
    //            ddlModule.DataBind();
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //        divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message);
    //    }
    //    finally { base.CloseConnection(); }
    //}
    private void resetText()
    {
        txtTitle.Text = "";
        txtUrl.Text = "";
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        base.OpenConnection();
        DataTable objDT;
        if (Request["a_id"] != null)
            objDT = BO_Ads_Picture.Search(Convert.ToInt32(Request["a_id"]));
        else
            objDT = null;
        string fileName = String.Empty;
        if (objDT != null && objDT.Rows.Count > 0)
            fileName = objDT.Rows[0]["URL"].ToString();
        try
        {
            if (fulAdv.HasFile)
            {
                fileName = fulAdv.FileName;
                int count = 0;
                while (File.Exists(Server.MapPath("~/upload/ads/") + fileName))
                {
                    fileName = count + "_" + fulAdv.FileName;
                    count++;
                }
                fulAdv.SaveAs(Server.MapPath("~/upload/ads/") + fileName);
            }
        }
        catch (Exception)
        {
            divMessage.InnerHtml = O2S_Message.Error("Hệ Thống", "Lỗi Ghi file!");
            return;
        }
        try
        {
            string title = txtTitle.Text.Trim();
            string url = txtUrl.Text.Trim();
            int module_id = Convert.ToInt32(ddlModule.SelectedValue);
            bool display = chbDisplay.Checked;
            bool isWap = false;
            int sortDisplay = Convert.ToInt32(txtSortDisplay.Text);
            DateTime endDate = cldEndDate.SelectedDate;
            DataTable admin = (DataTable)Session["Admin"];
            string result = string.Empty;
            if (objDT != null && objDT.Rows.Count > 0)
                result = BO_Ads_Picture.Update(Convert.ToInt32(objDT.Rows[0]["PICTURE_ID"]), module_id, title
                    , fileName, url, display, sortDisplay, Convert.ToDateTime(objDT.Rows[0]["CREATED_DATE"]), objDT.Rows[0]["CREATED_BY"].ToString(), DateTime.Now, endDate, admin.Rows[0]["USER_NAME"].ToString(), true);
            else
            {
                result = BO_Ads_Picture.Insert(module_id, title
                    , fileName, url, display, sortDisplay, DateTime.Now, admin.Rows[0]["USER_NAME"].ToString(), DateTime.Now, endDate, "", true);
                resetText();
            }
            if (result.Equals("0"))
                divMessage.InnerHtml = O2S_Message.Error("Thông Báo", "Lỗi trong quá trình truyền tải dữ liệu. Mong bạn trở lại sau .");
            else
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Thực hiện thành công .");
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi xảy ra !<br />" + ex.Message);
        }
        finally { base.CloseConnection(); }
    }
    protected void btnAdv_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx?module=AdvPicture");
    }
    private void loadSetting()
    {
        objDT = BO_Settings.SelectByGroupId(4, 1);
        foreach (DataRow dr in objDT.Rows)
        {
            string value = dr["SETTING_VALUE"].ToString();
            switch (dr["SETTING_KEY"].ToString())
            {
                case "NOTE_DISPLAY":
                    NOTE_DISPLAY.InnerText = value;
                    break;
                case "NOTE_MODULE":
                    NOTE_MODULE.InnerText = value;
                    break;
                case "NOTE_SORT":
                    NOTE_SORT.InnerText = value;
                    break;
                case "NOTE_TITLE":
                    NOTE_TITLE.InnerText = value;
                    break;
                case "NOTE_URL":
                    NOTE_URL.InnerText = value;
                    break;
                case "NOTE_LINK":
                    NOTE_LINK.InnerText = value;
                    break;
                case "NOTE_ENDDATE":
                    NOTE_ENDDATE.InnerText = value;
                    break;
            }
        }
    }
}
